package com.oocl.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONObject;

import com.oocl.constant.ApplyStatus;
import com.oocl.pojo.Merchant;
import com.oocl.service.MerchantManager;
import com.oocl.service.factory.MerchantManagerFactory;
import com.oocl.util.RequestUtil;

/**
 * Servlet implementation class ProcessServlet
 */
@WebServlet("/sec/process")
public class ProcessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantManager manager;
	private Logger logger = Logger.getLogger(ProcessServlet.class);
	
    public ProcessServlet() {
        super();
        manager = MerchantManagerFactory.getInstance();
    }
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("submit to process merchant apply");
		JSONObject json = RequestUtil.parseJsonParameters(request);
		response.setContentType("application/json;charset=utf-8");
		System.out.println(json.get("result")+","+json.get("content")+","+json.getString("id"));

		String result = json.getString("result");
		String id = json.getString("id");
		if("1".equals(result)){
			manager.updateStatus(id, ApplyStatus.APPROVED);
		}else if("2".equals(result)){
			String bcontent = json.getString("content");
			if(null==bcontent || "".equals(bcontent)){
				RequestUtil.sendResponse(false, "必须填写驳回理由", response);
				return;
			}
			if(bcontent.length()>100){
				RequestUtil.sendResponse(false, "驳回理由必须在一百字以内", response);
				return;
			}
			Merchant merchant = manager.findMerchantById(id);
			merchant.setbContent(bcontent);
			merchant.setStatus(ApplyStatus.REJECTED);
			manager.update(merchant);
		}
		RequestUtil.sendResponse(true, "审核成功", response);
	}
	
}
